<?php

namespace App\Http\Controllers\Api;

use App\Models\Coupon;
use App\Models\SellOrder;
use App\Models\User;
use App\Models\UserCoupon;
use App\Services\UserCouponService;
use Illuminate\Http\Request;

class CouponController extends BaseController
{
    public function newUserCoupon(Request $request)
    {
        $coupon = Coupon::query()->where('type',2)->first();

        $result = [
            'id'            => $coupon->id,
            'title'         => $coupon->title,
            'order_price'   => $coupon->order_price,
            'days'          => $coupon->days,
            'amount'        => $coupon->amount,
        ];
        return $this->ok($result);
    }

    public function getNewUserCoupon(Request $request)
    {
        $coupon_id = intval($request->get('coupon_id'));
        if(empty($coupon_id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $coupon = Coupon::find($coupon_id);
        if(empty($coupon) || $coupon->type != 2)
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = UserCoupon::query()->where('user_id',$this->user_id)->where('coupon_id',$coupon_id)->first();
        if(!empty($row))
        {
            return $this->err(1,200,'','已领取,请勿重复领取');
        }

        $userCouponService = new UserCouponService();
        $ret = $userCouponService->getCoupon($this->user_id,$coupon->type);
        if($ret)
        {
            return $this->ok([],'领取成功');
        }
        return $this->err(1,200,'','领取失败');
    }

    public function userCouponList(Request $request)
    {
        $page      = intval($request->get('page',1));
        $page      = max($page ,1);
        $page_size = intval($request->get('page_size',10));
        $type      = intval($request->get('type',1));  //1未使用 2已使用 3已过期

        if(empty($type) || !in_array($type,[1,2,3]))
        {
            return $this->err(1,200,'','参数错误');
        }

        $userCouponService = new UserCouponService();
        $ret = $userCouponService->getUserCouponList($this->user_id,$type,$page,$page_size);
        return $this->ok($ret);
    }

    public function sellCouponList()
    {
        $items = [];

        $order_num = SellOrder::query()->where('user_id',$this->user_id)->where('state',5)->count();
        $coupon_price = UserCoupon::query()->where('user_id',$this->user_id)->whereIn('coupon_type',[3,4,5,6])->sum('coupon_amount');
        $list = Coupon::query()->whereIn('type',[3,4,5,6])->get();
        foreach ($list as $v)
        {
            //判断是否已经领取过
            $is_get = 0;
            $row = UserCoupon::query()->where('user_id',$this->user_id)->where('coupon_id',$v->id)->first();
            if(!empty($row))
            {
                $is_get = 1;
            }

            $allow_get = 0;
            switch ($v->type)
            {
                case 3:
                    if($order_num >= 1)
                    {
                        $allow_get = 1;
                    }
                    break;
                case 4:
                    if($order_num >= 3)
                    {
                        $allow_get = 1;
                    }
                    break;
                case 5:
                    if($order_num >= 5)
                    {
                        $allow_get = 1;
                    }
                    break;
                case 6:
                    if($order_num >= 8)
                    {
                        $allow_get = 1;
                    }
                    break;
            }
            $items[] = [
                'id'            => $v->id,
                'order_price'   => $v->order_price,
                'days'          => $v->days,
                'type'          => $v->type,
                'amount'        => $v->amount,
                'title'         => $v->title,
                'is_get'        => $is_get,
                'allow_get'     => $allow_get,
            ];
        }

        $result = [
            'items'         => $items,
            'order_num'     => $order_num,
            'coupon_price'  => $coupon_price,
        ];
        return $this->ok($result);
    }

    public function getSellOrderCoupon(Request $request)
    {
        $coupon_id = intval($request->get('coupon_id'));
        if(empty($coupon_id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $coupon = Coupon::find($coupon_id);
        if(empty($coupon) || !in_array($coupon->type,[3,4,5,6]))
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = UserCoupon::query()->where('user_id',$this->user_id)->where('coupon_id',$coupon_id)->first();
        if(!empty($row))
        {
            return $this->err(1,200,'','已领取,请勿重复领取');
        }

        $order_num = SellOrder::query()->where('user_id',$this->user_id)->where('state',5)->count();

        $allow_get = 0;
        switch ($coupon->type)
        {
            case 3:
                if($order_num >= 1)
                {
                    $allow_get = 1;
                }
                break;
            case 4:
                if($order_num >= 3)
                {
                    $allow_get = 1;
                }
                break;
            case 5:
                if($order_num >= 5)
                {
                    $allow_get = 1;
                }
                break;
            case 6:
                if($order_num >= 8)
                {
                    $allow_get = 1;
                }
                break;
        }

        if(!$allow_get)
        {
            return $this->err(1,200,'','尚未达到领取条件');
        }

        $userCouponService = new UserCouponService();
        $ret = $userCouponService->getCoupon($this->user_id,$coupon->type);
        if($ret)
        {
            return $this->ok([],'领取成功');
        }
        return $this->err(1,200,'','领取失败');
    }
}
